package com.wali.modules.common.control;

import com.alibaba.fastjson.JSONObject;
import com.wali.common.ccp.exception.CcpException;
import com.wali.common.ccp.execl.CcpExcelReport;
import com.wali.common.ccp.execl.CcpExcelUtils;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.common.web.BaseController;
import com.wali.modules.bil.bo.BilExpressBO;
import com.wali.modules.bil.enums.BilExpressEnums;
import com.wali.modules.common.bo.ComGoodsBO;
import com.wali.modules.common.service.ComGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author walixu
 * @ClassName com.wali.modules.common.control
 * @Description 备案商品controller
 * @email wali.xu@kpmg.com
 * @date 09/03/2019 7:43 下午
 */
@Controller
@RequestMapping(value = "/com")
public class ComGoodsController extends BaseController {

    @Autowired
    private ComGoodsService comGoodsService;

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 导出符合条件的商品信息
     */
    @RequestMapping(value = "/exportComGoods")
    public void exportComGoods(BilExpressBO bilExpressBO, HttpServletResponse response) {
        try {
            CcpExcelReport report = comGoodsService.exportComGoods();
            writeExecl(response, report);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 导出符合条件的商品信息
     */
    @RequestMapping(value = "/importComGoods")
    public ResponseEntity<String> importComGoods(@RequestParam("comGoodsFile") MultipartFile file, HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            if (file.getSize() > 5000000) {// 不能超过5M
                throw new CcpException(BilExpressEnums.FILE_TOO_LARGE);
            }

            CcpExcelUtils excelUtils = new CcpExcelUtils(file, -1, 0);
            List<ComGoodsBO> comGoodsList = excelUtils.getDataList(ComGoodsBO.class);
            if (comGoodsList.size() > 1000) {
                throw new CcpException(BilExpressEnums.DATA_TOO_MUCH);
            }

            String count = comGoodsService.importComGoods(comGoodsList);
            JSONObject result = new JSONObject();
            result.put("saveCount", count.split(",")[0]);
            result.put("updateCount", count.split(",")[1]);
            result.put("deleteCount", count.split(",")[2]);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            return getResponseEntity(code, msg, result);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }
}
